package com.loser.excel.config;

import com.loser.excel.aspect.ExcelExportAspect;
import com.loser.excel.core.CmsExcelService;
import com.loser.excel.core.ExcelExportServer;
import com.loser.excel.core.handler.ExcelExportHandler;
import com.loser.excel.core.handler.upload.UploadFileHandler;
import com.loser.redis.core.RedisHandler;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * excel export 配置类
 *
 * @author alan
 * @date 2022/6/21 21:41
 */
@Configuration
@EnableConfigurationProperties({ExcelExportProperties.class})
public class ExcelExportConfiguration {

    private final ExcelExportProperties excelExportProperties;

    public ExcelExportConfiguration(ExcelExportProperties excelExportProperties) {
        this.excelExportProperties = excelExportProperties;
    }

    @Bean
    public ExcelExportAspect excelExportAspect(RedisHandler redisHandler) {
        return new ExcelExportAspect(redisHandler);
    }

    @Bean
    public CmsExcelService cmsExcelService() {
        return new CmsExcelService();
    }

    @Bean
    public UploadFileHandler uploadFileHandler() throws Exception {
        return (UploadFileHandler) Class.forName(excelExportProperties.getUpdateFileHandler()).newInstance();
    }

    @Bean
    public ExcelExportServer excelExportServer(RedisHandler redisHandler, ExcelExportHandler excelExportHandler,
                                               CmsExcelService cmsExcelService, UploadFileHandler uploadFileHandler) {
        return new ExcelExportServer(redisHandler, excelExportHandler, cmsExcelService, excelExportProperties, uploadFileHandler);
    }

    @Bean
    public ExcelExportHandler excelExportHandler(RedisHandler redisHandler) {
        return new ExcelExportHandler(redisHandler);
    }

}
